<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="roles" style="width: 100%" border stripe>
        <el-table-column width="30" type="expand">
          <template slot-scope="scope">
            <el-row  v-for="(item, index) in scope.row.children" :key="item.id" class="border_buttom row_center" :class="index === 0 ? 'border_top':''">
              <el-col :span="8"><el-tag type="primary">{{item.authName}}</el-tag><i class="el-icon-caret-right"></i></el-col>
              <el-col :span="16">
                <el-row class="row_center" :class="index2===0?'':'border_top'" v-for="(item2,index2) in item.children" :key="item2.id">
                  <el-col :span="5">
                    <el-tag type="success">{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="19" v-for="item3 in item2.children" :key="item3.id">
                    <el-tag closable type="warning" @close="removeTag(scope.row,item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column prop="date" label="操作"  width="300">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRight(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 显示分配权限的对话框 -->
    <el-dialog title="提示" :visible.sync="setRightDialogVisible" width="50%" @close="closeSetDialog">
      <el-tree ref="tree" :default-checked-keys='checkeds' default-expand-all node-key="id" :data="rights" :props="defaultProp" show-checkbox> </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSetRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      roles: [],
      setRightDialogVisible: false,
      rights: [],
      defaultProp: {
        label: 'authName'
      },
      // 被选中的id值
      checkeds: [],
      roleId: ''
    }
  },
  methods: {
    // 获取权限列表
    async getRoles () {
      const { data: res } = await this.$axios.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取用户权限列表失败！')
      this.roles = res.data
      // console.log(res.data)
    },
    // 关闭标签
    removeTag (row, rightId) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`roles/${row.id}/rights/${rightId}`)
        if (res.meta.status !== 200) return this.$$message.error('删除权限失败！')
        row.children = res.data
        this.$message.success('删除成功!')
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    // 显示设置权限对话框
    async showSetRight (row) {
      this.setRightDialogVisible = true
      const { data: res } = await this.$axios.get('rights/tree')
      if (res.meta.status !== 200) return this.$message('获取用户权限列表失败！')
      this.rights = res.data
      this.getAllKes(row)
      this.roleId = row.id
    },
    // 获取所有的树形的被选择id
    getAllKes (row) {
      if (!row.children) return this.checkeds.push(row.id)
      row.children.forEach(item => {
        this.getAllKes(item)
      })
    },
    // 置空上次选择的tree的id
    closeSetDialog () {
      this.checkeds = []
    },
    async submitSetRights () {
      const allCheckedKey = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()].join(',')
      // console.log(allCheckedKey)
      const { data: res } = await this.$axios.post('roles/' + this.roleId + '/rights', { rids: allCheckedKey })
      console.log(res, this.roleId)
      if (res.meta.status !== 200) return this.$message.error('添加权限失败！')
      this.setRightDialogVisible = false
      this.$message.success('更新成功！')
    }
  },
  created () {
    this.getRoles()
  }
}
</script>

<style lang='less' scoped>
.el-tag{
  margin: 10px;
}
.border_top{
  border-top: 1px solid #eee;
}
.border_buttom{
  border-bottom: 1px solid #eee;
}
.row_center{
  display: flex;
  align-items: center;
}
</style>
